package cn.kgc.tangcco.service.impl;

import cn.kgc.tangcco.mapper.StudentMapper;
import cn.kgc.tangcco.model.PageParam;
import cn.kgc.tangcco.model.ResponseText;
import cn.kgc.tangcco.model.ResultCode;
import cn.kgc.tangcco.pojo.Clazz;
import cn.kgc.tangcco.pojo.Student;
import cn.kgc.tangcco.service.StudentService;
import cn.kgc.tangcco.utils.cryptography.BaseCryptographyUtils;
import cn.kgc.tangcco.vo.StudentVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/12/26 17:10
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private Student student;
    @Value("${key.des}")
    private String deskey;

    @Autowired
    private StudentMapper studentMapper;

    @Transactional
    @Override
    public ResponseText add(List<Student> students) {
        if (students == null || students.size() == 0) {
            return new ResponseText(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), null);
        }
        if (studentMapper.insertStudent(students) > 0) {
            return new ResponseText(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
        }
        return new ResponseText(ResultCode.DATA_ERROR.getCode(), ResultCode.DATA_ERROR.getMsg(), null);
    }

    @Transactional
    @Override
    public ResponseText addStudent(Student student) {
        if (studentMapper.insert(student) > 0) {
            return new ResponseText(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
        }
        return new ResponseText(ResultCode.DATA_ERROR.getCode(), ResultCode.DATA_ERROR.getMsg(), null);
    }

    @Override
    public Page<Student> query(Map<String, Object> paramMap) {
        // 从查询参数中获取分页参数对象
        PageParam pageParam = (PageParam) paramMap.get("pageParam");
        // 传入查询页码和每页记录数 注意代码顺序
        Page<Student> studentPage = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        student = (Student) paramMap.get("student");
        if (student.getCid() != null && student.getCid() != 0) {
            queryWrapper.eq("cid", student.getCid());
        }
        if (student.getFirstName() != null) {
            queryWrapper.like("first_name", student.getFirstName());
        }
        queryWrapper.orderByDesc("id");
        return student.selectPage(studentPage, queryWrapper);
    }

    @Override
    public PageInfo<StudentVo> queryVo(Map<String, Object> paramMap) {
        // 从查询参数中获取分页参数对象
        PageParam pageParam = (PageParam) paramMap.get("pageParam");
        // 传入查询页码和每页记录数 注意代码顺序
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), "s.id desc");
        List<StudentVo> students = studentMapper.selectStudentVo((Student) paramMap.get("student"));
        for (int i = 0; i < students.size(); i++) {
            StudentVo studentVo = students.get(i);
            studentVo.convertIdCard();
            students.set(i, studentVo);
        }
        return new PageInfo<>(students);
    }

    @Override
    public ResponseText queryMobileExists(String mobile) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("mobile", "15311484568");
        if (studentMapper.selectCount((queryWrapper)) > 0) {
            return new ResponseText(ResultCode.MOBILE_EXISTS.getCode(), ResultCode.MOBILE_EXISTS.getMsg(), null);
        }
        return new ResponseText(ResultCode.MOBILE_NOT_EXISTS.getCode(), ResultCode.MOBILE_NOT_EXISTS.getMsg(), null);
    }

    @Transactional
    @Override
    public ResponseText delelteStudent(List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            return new ResponseText(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), false);
        }
        if (ids.size() == 1){
            student.setId(ids.get(0));
            if (student.deleteById()){
                return new ResponseText(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), true);
            }else{
                return new ResponseText(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), false);
            }
        }
        if (studentMapper.deleteBatchIds(ids) > 0) {
            return new ResponseText(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), true);
        }
        return new ResponseText(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), false);
    }

    @Override
    public ResponseText<Student> getStudentById(Integer id) {
        if (id == null || id == 0) {
            return new ResponseText<>(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), null);
        }
        student.setId(id);
        Student newStudent = student.selectById();
        if (newStudent != null) {
            newStudent.setAuthText(BaseCryptographyUtils.desDecodeHexString(newStudent.getAuthText(), deskey));
            return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), newStudent);
        }
        return new ResponseText(ResultCode.DATA_EMPTY.getCode(), ResultCode.DATA_EMPTY.getMsg(), null);
    }

    @Transactional
    @Override
    public ResponseText updateStudent(Student student) {
        if (student == null || student.getId() == null || student.getAuthText() == null) {
            return new ResponseText(ResultCode.PARAM_ERROR.getCode(), ResultCode.PARAM_ERROR.getMsg(), null);
        }
        student.setAuthText(BaseCryptographyUtils.desEncodeHexString(student.getAuthText(), deskey));
        if (studentMapper.updateById(student) > 0) {
            return new ResponseText(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), null);
        }
        return new ResponseText(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), null);
    }

    @Override
    public List<StudentVo> queryCountGroupByGender(Clazz clazz) {
        return studentMapper.selectCountGroupByGender(clazz);
    }

    @Override
    public ResponseText<Boolean> addOrUpdate(Student student) {
        student.setAuthText(BaseCryptographyUtils.desEncodeHexString(student.getAuthText(), deskey));
        if (student.insertOrUpdate()) {
            return new ResponseText<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), true);
        } else {
            return new ResponseText<>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg(), false);
        }
    }
}
